package com.awanp3.nimbus.remote.service;

import cn.hutool.http.HttpUtil;
import com.awanp3.nimbus.common.json.utils.JsonUtils;
import com.awanp3.nimbus.remote.api.HWorkTokenService;

import com.awanp3.nimbus.remote.properties.HWorkProperties;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import cn.hutool.json.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

/**
 * @author 20014170
 */
@RequiredArgsConstructor
@Service
@DubboService
public class HWorkTokenServiceImpl implements HWorkTokenService {

    private final HWorkProperties hWorkProperties;

    @Override
    public String analyze(String token) {
        if (hWorkProperties.isMockEnabled()){
            // 模拟登录用户
            JSONObject data = new JSONObject();
            data.set("userCode", "20014170");
            data.set("userName", "邵京凯");
            JSONObject result = new JSONObject();
            result.set("code", 200);
            result.set("msg", "处理成功!");
            result.set("data", data);
            return JsonUtils.toJsonString(result);
        }

        String url = this.hWorkProperties.getBaseUrl().concat("/domain/api/v2/me");
        return HttpUtil.createGet(url)
            .header(HttpHeaders.AUTHORIZATION, token)
            .header(HttpHeaders.CONTENT_TYPE,"application/json")
            .execute().body();
    }
}
